package edu.ts.crowdfunding.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.ts.crowdfunding.domain.User;
import edu.ts.crowdfunding.mapper.UserMapper;
import edu.ts.crowdfunding.product.domain.PO.ConditionalQueryPO;
import edu.ts.crowdfunding.product.domain.PO.ProductPO;
import edu.ts.crowdfunding.product.domain.Product;
import edu.ts.crowdfunding.product.service.ProductService;
import edu.ts.crowdfunding.product.mapper.ProductMapper;
import edu.ts.crowdfunding.service.UserService;
import edu.ts.crowdfunding.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService{

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result pageSearch(Integer pageNum, Integer pageSize, ConditionalQueryPO conditionalQueryPO) {
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Product> list = productMapper.pageSearch(conditionalQueryPO);
            PageInfo<Product> pageInfo = new PageInfo<>(list);
            return new Result(1,"查询成功",pageInfo);
        } catch (Exception e) {
            return new Result(0,e.getMessage(),null);
        }
    }

    @Override
    public Result add(ProductPO productPO) {
        if (productPO.getTitle() == null || productPO.getTitle().equals("")){
            return new Result(0,"标题不能为空");
        }
        if (productPO.getSubtitle() == null || productPO.getSubtitle().equals("")){
            return new Result(0,"副标题不能为空");
        }
        if (productPO.getCreatorId() == null){
            return new Result(0,"创建者id不能为空");
        }
        if (productPO.getCategoryId()== null){
            return new Result(0,"分类id不能为空");
        }
        if (productPO.getGoalAmount() == null){
            return new Result(0,"目标金额不能为空");
        }
        Product product = new Product();
        BeanUtils.copyProperties(productPO, product);
        product.setStartTime(LocalDateTime.now());
        product.setEndTime(LocalDateTime.now().plus(7, java.time.temporal.ChronoUnit.DAYS));
        product.setCreatedAt(LocalDateTime.now());
        //TODO 可能存在的管理员ID
        //验证用户是否存在
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserId, productPO.getCreatorId()));
        if (user == null){
            return new Result(0,"用户不存在");
        }
        //验证分类是否存在
        Long category = productMapper.getCategory(product.getCategoryId());
        if (category <= 0){
            return new Result(0,"分类不存在");
        }
        int count = productMapper.insert(product);
        return count == 0 ? new Result<String>(0,"添加失败",""): new Result<String>(1,"添加成功","");
    }

    @Override
    public Product getProductRef(Long id) {
        return productMapper.getProductRef(id);
    }

    @Override
    public void deleteProductById(Long id) {
        productMapper.deleteProductById(id);
    }
}




